package com.oocl.consume.dutil;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.apache.commons.dbcp.BasicDataSource;

public class DBUtil {
	
	private static final String className = "oracle.jdbc.OracleDriver";//数据库驱动
	private static final String url = "jdbc:oracle:thin:@127.0.0.1:1521:xe";
	private static final String username="wyatt";
	private static final String psd="123456";
	private static Connection con;
	private static BasicDataSource ds;
	
	static{
		ds=new BasicDataSource();
		ds.setUrl(url);
		ds.setDriverClassName(className);
		ds.setUsername(username);
		ds.setPassword(psd);
	}
	public static Connection createConnectionWithDatasource(){
		Connection con = null;
		try {
			con = ds.getConnection();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return con;
	}
	
	
	public static Connection createConnection(){
		con = null;
		try {
			Class.forName(className);//类加载
			con = DriverManager.getConnection(url,username,psd);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return con;
	}
	
	public static void close(Connection con,PreparedStatement pst,ResultSet rs){
		try {
			if(con!=null)
			con.close();
			if(pst!=null)
			pst.close();
			if(rs!=null)
			rs.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
